package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageBean;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * DishServiceImpl
 *
 * @author zxh
 * @date 2024/2/1
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public DishVO getDish(String id) {

        //根据id查询菜品数据
        Dish dish = dishMapper.getDish(id);

        //根据菜品id查询口味数据
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);

        //将查询到的数据封装到VO
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;

    }

    @Override
    public List<Dish> getDishList(String categoryId) {
        return dishMapper.getDishList(categoryId);
    }

    @Transactional
    @Override
    public void changeStatus(String status, String id) {
        //获取套餐ids
        List<Integer> setmeal_ids = setmealDishMapper.getSetmealByDishId(id);
        if (Integer.valueOf(status).equals(StatusConstant.DISABLE) && setmeal_ids.size()>0) {
            // 将这些套餐状态改为停售
            // 获取setmeals
            List<Setmeal> setmeals = setmealMapper.getSetmealByIds(setmeal_ids);
            for (Setmeal setmeal : setmeals) {
                setmeal.setStatus(StatusConstant.DISABLE);
                setmealMapper.changeStatus(setmeal);
            }
        }
        Long currentId = BaseContext.getCurrentId();
        dishMapper.changeStatus(status, id, currentId);
    }

    @Override
    public PageBean<DishVO> pageDish(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageBean<>(page.getResult(), page.getTotal());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateDish(DishDTO dishOTD) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishOTD, dish);
        // 更新菜品
        dishMapper.updateDish(dish);

        List<DishFlavor> flavors = dishOTD.getFlavors();
        // 2.1 批量删除当前菜品所有口味
        dishFlavorMapper.deleteByDishIds(Collections.singletonList(String.valueOf(dishOTD.getId())));
        // 2.修改口味
        if (flavors != null && !flavors.isEmpty()) {
            // 2.2 批量添加口味
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishOTD.getId());
            }
            dishFlavorMapper.addDishFlavor(flavors);
            //dishFlavorMapper.updateDishFlavors(flavors);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.addDish(dish);
        // 获取dish的ID  主键返回  insert id="addDish" useGeneratedKeys="true" keyProperty="id"
        // 添加口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.addDishFlavor(flavors);
        }


    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteDishes(List<String> ids) {
        // 1. 判断当前菜品是否在起售中
        Integer startCount = dishMapper.countStart(ids);
        if (startCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        // 2. 判断是否在 套餐管理中
        Integer dishCount = dishFlavorMapper.countDish(ids);
        if (dishCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //3. 删除菜品
        dishMapper.deleteInBulk(ids);
        // 4.删除菜品对应的口味

        dishFlavorMapper.deleteByDishIds(ids);
    }

    @Override
    public List<DishVO> getDishFlavorList(String categoryId) {
        List<Dish> dishList = dishMapper.getDishListStstusTrue(categoryId);

        ArrayList<DishVO> dishVOS = new ArrayList<>();
        for (Dish dish : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(String.valueOf(dish.getId()));

            dishVO.setFlavors(flavors);
            dishVOS.add(dishVO);
        }
        return dishVOS;
    }

    @Override
    public List<DishItemVO> getSetmealDishByIds(String id) {
      List<DishItemVO> list=  setmealMapper.getSetmealDishByIds(id);
      return list;
    }


}
